using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Passport_PasswordManager : Member, IRequiresSessionState
{
	protected UserControls_cmpy_Top Top1;
	protected Panel NoSetQuestionMsg;
	protected Panel ModifyQuestionMsg;
	protected DropDownList H_SaveQuestion;
	protected TextBox H_SaveAnswer;
	protected TextBox H_UserEmail;
	protected Label ChangeQuestionAnswer_tips;
	protected Button Submit1;
	protected Panel SaveQuestionZone;
	protected HtmlGenericControl fieldId;
	protected Literal H_UserQuestion;
	protected TextBox H_UserAnswer;
	protected Label err_tips;
	protected Button ConfirmAnswerRight;
	protected Button returnBt2;
	protected Panel EditQuestionZone;
	protected HtmlGenericControl fieldzq;
	protected Panel ModifyQuestionZone;
	protected TextBox IDNum;
	protected Panel CheckUserIdentify;
	protected TextBox H_OldPassword;
	protected TextBox H_NewPassword;
	protected TextBox H_RePassword;
	protected Label ChangePassword_err_tips;
	protected RequiredFieldValidator RequiredFieldValidator1;
	protected CompareValidator CompareValidator1;
	protected Button ChangePassword;
	protected Button returnBt;
	protected Panel ChangePasswordZone;
	protected UserControls_CMYP_Foot Foot1;
	protected HtmlForm form1;
	private UserBll _user;
	private string other = "";
	private string m_ComeUrl = "";
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected UserBll m_User
	{
		get
		{
			UserBll user;
			if (this._user == null)
			{
				this._user = new UserBll(this.conn);
				user = this._user;
			}
			else
			{
				user = this._user;
			}
			return user;
		}
	}
	protected void Page_PreInit(object sender, EventArgs e)
	{
		this.SelectAction();
		try
		{
		}
		catch (Exception ex)
		{
			base.WriteTemErrMsg(ex.Message.ToString());
		}
	}
	protected void SelectAction()
	{
		if (this.ModifyQuestionPost())
		{
			this.ModifyQuestion();
		}
		else
		{
			if (this.QuestionSavePost())
			{
				this.SaveQuestion();
			}
			else
			{
				if (this.ModifyPWDPost())
				{
					this.ModifyPWD();
				}
			}
		}
	}
	protected bool ModifyQuestionPost()
	{
		return Fetch.GetOrPost("H_UserAnswer") != string.Empty;
	}
	protected bool QuestionSavePost()
	{
		return Fetch.GetOrPost("H_SaveQuestion") != string.Empty && Fetch.GetOrPost("H_SaveAnswer") != string.Empty && Fetch.GetOrPost("H_UserEmail") != string.Empty;
	}
	protected bool ModifyPWDPost()
	{
		return Fetch.GetOrPost("H_OldPassword") != string.Empty && Fetch.GetOrPost("H_NewPassword") != string.Empty && Fetch.GetOrPost("H_RePassword") != string.Empty;
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (Fetch.Get("other") != string.Empty)
		{
			this.other = Fetch.Get("other");
		}
		this.m_ComeUrl = Text.GetComeUrl(Fetch.Get("ComeUrl"));
		if (this.m_ComeUrl == string.Empty)
		{
			this.m_ComeUrl = this.config["SiteUrl"].ToString();
		}
		if (!base.IsPostBack)
		{
			if (Fetch.Get("action") == "edit")
			{
				this.ModifyQuestionZone.Visible = true;
				this.ChangePasswordZone.Visible = false;
				this.H_SaveQuestion.DataSource = this.GetQuestionList();
				this.H_SaveQuestion.DataBind();
				DataTable userByUserID = this.m_User.GetUserByUserID(base.user.UserID);
				if (userByUserID.Rows.Count <= 0)
				{
					this.WriteErrMsg("", Language.Get("Common/NotData"), "");
				}
				if (userByUserID.Rows[0]["Question"].ToString() == string.Empty || userByUserID.Rows[0]["Answer"].ToString() == string.Empty)
				{
					this.Session["QuestionAnswerID"] = base.user.UserID.ToString();
					this.SaveQuestionZone.Visible = true;
					this.EditQuestionZone.Visible = false;
				}
				this.H_UserQuestion.Text = userByUserID.Rows[0]["Question"].ToString();
			}
			else
			{
				this.ModifyQuestionZone.Visible = false;
				this.ChangePasswordZone.Visible = true;
			}
		}
	}
	protected void ModifyQuestion()
	{
		string text = Fetch.Post("H_UserAnswer");
		DataTable userByUserID = this.m_User.GetUserByUserID(base.user.UserID);
		if (userByUserID.Rows.Count <= 0)
		{
			this.WriteErrMsg("", Language.Get("Common/NotData"), "");
		}
		if (userByUserID.Rows[0]["Question"].ToString() == string.Empty || userByUserID.Rows[0]["Answer"].ToString() == string.Empty)
		{
			this.Session["QuestionAnswerID"] = base.user.UserID.ToString();
			this.SaveQuestionZone.Visible = true;
			this.fieldId.Visible = true;
			this.EditQuestionZone.Visible = false;
		}
		else
		{
			text = this.Md5(text, 32);
			if (!Text.CheckMd5Text(userByUserID.Rows[0]["Answer"].ToString(), text))
			{
				this.WriteErrMsg("", Language.Get("PassPort/AnswerErr"), "");
			}
			this.Session["QuestionAnswerID"] = base.user.UserID.ToString();
			this.SaveQuestionZone.Visible = true;
			this.fieldId.Visible = true;
			this.fieldzq.Visible = false;
			this.EditQuestionZone.Visible = false;
			this.H_UserEmail.Text = userByUserID.Rows[0]["Email"].ToString();
		}
	}
	protected void SaveQuestion()
	{
		string orPost = Fetch.GetOrPost("H_SaveQuestion");
		string text = Fetch.GetOrPost("H_SaveAnswer");
		string text2 = Fetch.GetOrPost("H_UserEmail");
		if (orPost == string.Empty)
		{
			this.WriteErrMsg("", Language.Get("Common/Fail", "请选择问题！"), "");
		}
		if (this.Session["QuestionAnswerID"] == null)
		{
			this.WriteErrMsg("", Language.Get("Common/Fail", ""), "?action=edit");
		}
		if (this.Session["QuestionAnswerID"].ToString() != base.user.UserID.ToString())
		{
			this.Session["QuestionAnswerID"] = null;
			this.WriteErrMsg("", Language.Get("Common/Fail", ""), "?action=edit");
		}
		DataTable userByUserID = this.m_User.GetUserByUserID(base.user.UserID);
		if (userByUserID.Rows.Count <= 0)
		{
			this.WriteErrMsg("", Language.Get("Common/NotData"), "");
		}
		if (text2.ToLower() == userByUserID.Rows[0]["Email"].ToString().ToLower())
		{
			text2 = "";
		}
		text = this.Md5(text, 32);
		if (this.m_User.UpdatePassport(base.user.UserID, "", text2, orPost, text))
		{
			this.Session["QuestionAnswerID"] = null;
			if (this.config["HLIntegration"].ToString() == "1")
			{
				new Integrate(this.conn)
				{
					ComeUrl = this.m_ComeUrl
				}.Send("Modify", base.user.UserName, base.user.Password, text2, orPost, text, 0, base.user.Password, this.other);
			}
			else
			{
				if (this.m_ComeUrl == "")
				{
					this.m_ComeUrl = "/";
				}
				this.WriteSuccessMsg("", Language.Get("Common/Success"), this.m_ComeUrl);
			}
		}
		else
		{
			this.WriteErrMsg("", Language.Get("Common/Fail"), "");
		}
	}
	public int checkpassword(string password)
	{
		Regex regex = new Regex("^[A-Za-z]+$");
		int result;
		if (regex.IsMatch(password))
		{
			result = 1;
		}
		else
		{
			if (Regex.IsMatch(password, "^([+-]?)\\d*$"))
			{
				result = 2;
			}
			else
			{
				char[] array = password.ToCharArray();
				if (password.Length < 6 || password.Length > 20)
				{
					result = 0;
				}
				else
				{
					int i;
					for (i = 0; i < password.Length; i++)
					{
						if (array[0] != array[i])
						{
							break;
						}
					}
					if (i == array.Length)
					{
						result = 3;
					}
					else
					{
						string text = "01234567890";
						if (text.IndexOf(password) != -1)
						{
							result = 4;
						}
						else
						{
							string text2 = "abcdefghijklmnopqrstuvwxyz";
							if (text2.IndexOf(password) != -1)
							{
								result = 5;
							}
							else
							{
								string text3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
								if (text3.IndexOf(password) != -1)
								{
									result = 6;
								}
								else
								{
									result = -1;
								}
							}
						}
					}
				}
			}
		}
		return result;
	}
	protected void ModifyPWD()
	{
		string text = Fetch.GetOrPost("H_OldPassword");
		string text2 = Fetch.GetOrPost("H_NewPassword");
		string orPost = Fetch.GetOrPost("H_RePassword");
		if (this.checkpassword(text2) == 0 && this.checkpassword(text2) != -1)
		{
			this.Page.ClientScript.RegisterStartupScript(base.GetType(), "Test", "<script>alert('密码长度不对!');</script>");
		}
		else
		{
			if (this.checkpassword(text2) == 1 && this.checkpassword(text2) != -1)
			{
				this.Page.ClientScript.RegisterStartupScript(base.GetType(), "Test", "<script>alert('不能全部是字符!');</script>");
			}
			else
			{
				if (this.checkpassword(text2) == 2 && this.checkpassword(text2) != -1)
				{
					this.Page.ClientScript.RegisterStartupScript(base.GetType(), "Test", "<script>alert('不能全部是数字!');</script>");
				}
				else
				{
					if (this.checkpassword(text2) == 3 && this.checkpassword(text2) != -1)
					{
						this.Page.ClientScript.RegisterStartupScript(base.GetType(), "Test", "<script>alert('您设置的密码有误，不能设置连续相同的字符或数字');</script>");
					}
					else
					{
						if (this.checkpassword(text2) == 4 || this.checkpassword(text2) == 5 || this.checkpassword(text2) == 6)
						{
							this.Page.ClientScript.RegisterStartupScript(base.GetType(), "Test", "<script>alert('您设置的密码有误，密码应该由6-16个英文字母、数字或符号组成!');</script>");
						}
						else
						{
							if (text2 != orPost)
							{
								this.WriteErrMsg("", Language.Get("PassPort/rePasswordErr"), "");
							}
							text = this.Md5(text, 32);
							DataTable userByUserID = this.m_User.GetUserByUserID(base.user.UserID);
							if (userByUserID.Rows.Count <= 0)
							{
								this.WriteErrMsg("", Language.Get("Common/NotData"), "");
							}
							if (!Text.CheckMd5Text(userByUserID.Rows[0]["Password"].ToString(), text))
							{
								this.WriteErrMsg("", Language.Get("PassPort/PasswordErr"), "");
							}
							text2 = this.Md5(text2, 32);
							if (this.m_User.UpdatePassport(base.user.UserID, text2, "", "", ""))
							{
								if (this.config["HLIntegration"].ToString() == "1")
								{
									new Integrate(this.conn)
									{
										ComeUrl = this.m_ComeUrl
									}.Send("Modify", base.user.UserName, text2, "", "", "", 0, base.user.Password, this.other);
								}
								else
								{
									this.WriteSuccessMsg("", Language.Get("Common/Success"), this.m_ComeUrl);
								}
							}
							else
							{
								this.WriteErrMsg("", Language.Get("Common/Fail"), "");
							}
						}
					}
				}
			}
		}
	}
	protected void returnBt_Click(object sender, EventArgs e)
	{
		base.Response.Redirect("/Member/PassportIndex.aspx");
	}
	protected void returnBt2_Click(object sender, EventArgs e)
	{
		base.Response.Redirect("/Member/PassportIndex.aspx");
	}
}
